package net.daum.webtoon.android;

import java.util.ArrayList;

import net.daum.webtoon.android.jibx.data.FileManager;
import net.daum.webtoon.android.jibx.data.ImageItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;

public class MainActivity3 extends Activity {

	public static String TAG = MainActivity3.class.getSimpleName();
	private WebView mView;
	private ListView mListView;
	private final String TOONRSS = "http://cartoon.media.daum.net/webtoon/rss/";
	private final String TOONDOMAIN = "http://cartoon.media.daum.net/";
	public static ArrayList<WebToonInfo> gInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_item);

		mView = (WebView) findViewById(R.id.content);

	}

	private class WebViewClientClass extends WebViewClient {

		@Override
		public void onPageFinished(WebView view, String url) {
			if (WebToonListActivity.MODE.equalsIgnoreCase(getString(R.string.menu_save)))
				startActivity(new Intent(MainActivity3.this, ContentListActivity.class).putExtra("GETNEXT", true).addFlags(
						Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT));
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		Display display = ((WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int displayWidth = display.getWidth();

		String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getApplicationContext().getPackageName() + "/"
				+ FileManager.TOONSPATH + MainActivity2.mToonItem.id;
		mView.setWebViewClient(new WebViewClientClass());
		mView.getSettings().setAllowFileAccess(true);
		mView.getSettings().setJavaScriptEnabled(true);
		mView.setPadding(0, 0, 0, 0);
		String conten = "";
		DisplayMetrics metrics = new DisplayMetrics();
		float DISPDPI = 0;
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		DISPDPI = metrics.density;
		for (ImageItem item : MainActivity2.mToonItem.images) {
			int width = item.width;
			int height = item.height;
			int newHeight = height;

			float widthScale = displayWidth / (float) width;
			newHeight = (int) (height * widthScale);
			conten += "<img src=\"file://" + path + "/" + makeFileName(item.url) + "\"  " + "width=\""
					+ (int) (((displayWidth / DISPDPI) / 10d) * 10) * 0.95 + "\" height=\"" + (int) (((newHeight / DISPDPI) / 10d) * 10) * 0.95
					+ "\" style=\"margin-left: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;\" />";
		}

		String htmlContent = "<html><title>"
				+ MainActivity2.mToonItem.title
				+ "<head>"
				+ "<style type=\"text/css\" >#wrapper {margin : 0 auto ;border:0px solid #000;}body {margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;}</style>"
				+ "</head></title><meta name=\"viewport\"\"content=\"width=" + (int) (((displayWidth / DISPDPI) / 10d) * 10) * 0.95
				+ ", initial-scale=1.0 ,user-scalable=no\" /><body><div id=\"wrapper\">" + conten.trim() + "</div></body></html>";
		mView.loadDataWithBaseURL("not_needed", htmlContent, "text/html", "utf-8", "not_needed");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private String makeFileName(String aUrl) {
		String[] returnStrs = aUrl.split("/");
		return returnStrs[returnStrs.length - 1] + ".jpg";
	}

}
